public class Solution {
    public List<List<Integer>> levelOrder(TreeNode root) {
        Queue<TreeNode> queue = new LinkedList<>();
        List<List<Integer>> lists = new ArrayList<>();
        TreeNode node = root;
        if(node == null) {
            return lists;
        }
        queue.add(node);

        do {
            List<Integer> list = new ArrayList<>();
            Queue<TreeNode> queue2 = new LinkedList<>();
            while(!queue.isEmpty()) {
                node = queue.poll();
                list.add(node.val);

                if(node.left != null) {
                    queue2.add(node.left);
                }
                if(node.right != null) {
                    queue2.add(node.right);
                }
            }
            lists.add(list);
            queue = queue2;
        } while(!queue.isEmpty());

        return lists;
    }
}